package com.sskj.fabi.ui.activity;


import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.R;
import com.sskj.fabi.R2;
import com.sskj.fabi.bean.PayTypeEnum;
import com.sskj.fabi.bean.PayWayBean;
import com.sskj.fabi.presenter.PublishActivityPresenter;
import com.sskj.fabi.util.TipFabiUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.TipUtil;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;

@Route(path = RConfig.FABI_PUBLISH)
public class PublishActivity extends BaseActivity<PublishActivityPresenter> {
    @BindView(R2.id.tvType)
    TextView tvType;
    @BindView(R2.id.ivDetail)
    ImageView ivDetail;
    @BindView(R2.id.tvTip)
    TextView tvTip;
    @BindView(R2.id.etNum)
    EditText etNum;
    @BindView(R2.id.etMin)
    EditText etMin;
    @BindView(R2.id.etMax)
    EditText etMax;
    @BindView(R2.id.tvPrice)
    TextView tvPrice;
    @BindView(R2.id.tvAddPayType)
    TextView tvAddPayType;
    @BindView(R2.id.llYl)
    LinearLayout llYl;
    @BindView(R2.id.llWx)
    LinearLayout llWx;
    @BindView(R2.id.llAlipay)
    LinearLayout llAlipay;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @BindView(R2.id.llPayType)
    LinearLayout llPayType;
    @Autowired
    boolean isBuy = false;
    private BottomSheetDialog typeSheet;
    private PayTypeEnum payTypeYL = PayTypeEnum.YL.setChoose(false);
    private PayTypeEnum payTypeWX = PayTypeEnum.WX.setChoose(false);
    private PayTypeEnum payTypeAL = PayTypeEnum.ALIPAY.setChoose(false);
    private String buyPrice = "";
    private String sellPrice = "";

    @Override
    protected int getLayoutResId() {
        return R.layout.fabi_activity_publish;
    }

    @Override
    public PublishActivityPresenter getPresenter() {
        return new PublishActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        tvType.setText(isBuy ? "发布求购" : "发布出售");
        if (isBuy) {
            changeBuy();
        } else {
            changeSell();
        }

        ClickUtil.click(ivDetail, () -> {
            ARouter.getInstance().build(RConfig.FABI_BUY_AND_SELL).withBoolean(Constans.IS_BUY, isBuy).navigation();
        });
        ClickUtil.click(tvAddPayType, () -> {
            ARouter.getInstance().build(RConfig.MINE_SECURITY_CENTER).navigation();
        });
        ClickUtil.click(tvType, () -> {
            if (typeSheet == null) {
                typeSheet = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, v) -> {
                    typeSheet.dismiss();
                    if (isBuy && position == 0) {
                        changeSell();

                    } else if (!isBuy && position == 1) {
                        changeBuy();
                    }
                }, "发布出售", "发布求购");
            }
            typeSheet.show();
        });
        ClickUtil.click(llYl, () -> {
            payTypeYL.setChoose(!payTypeYL.isChoose());
            llYl.setBackground(ContextCompat.getDrawable(App.INSTANCE, payTypeYL.isChoose() ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_strokedde));
        });
        ClickUtil.click(llWx, () -> {
            payTypeWX.setChoose(!payTypeWX.isChoose());
            llWx.setBackground(ContextCompat.getDrawable(App.INSTANCE, payTypeWX.isChoose() ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_strokedde));

        });
        ClickUtil.click(llAlipay, () -> {
            payTypeAL.setChoose(!payTypeAL.isChoose());
            llAlipay.setBackground(ContextCompat.getDrawable(App.INSTANCE, payTypeAL.isChoose() ? R.drawable.fabi_shape_corner10_strokeff3 : R.drawable.fabi_shape_corner10_strokedde));

        });
        ClickUtil.click(btSubmit, () -> {
            if (checkParam()) {
                TipFabiUtil.showPublish(this, isBuy,
                        tvPrice.getText().toString(),
                        etNum.getText().toString(), payPwd -> {

                            mPresenter.requsetPublish(isBuy ? "1" : "2", etNum.getText().toString(),
                                    etMin.getText().toString(),
                                    etMax.getText().toString(),
                                    tvPrice.getText().toString(),
                                    getPayType(), payPwd

                            );
                        });

            }
        });
    }

    private void changeBuy() {
        isBuy = true;
        tvType.setText("发布求购");
        etNum.setHint("发布求购数量");
        etNum.setText("");
        etMin.setText("");
        etMax.setText("");
        tvPrice.setText(isBuy ? buyPrice : sellPrice);
        llPayType.setVisibility(View.GONE);
        btSubmit.setText("求 购");
        btSubmit.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.fabi_shape_corner40_f53));
    }

    private void changeSell() {
        isBuy = false;
        tvType.setText("发布出售");
        etNum.setHint("发布出售数量");
        etNum.setText("");
        etMin.setText("");
        etMax.setText("");
        tvPrice.setText(isBuy ? buyPrice : sellPrice);
        llPayType.setVisibility(View.VISIBLE);
        btSubmit.setText("出 售");
        btSubmit.setBackground(ContextCompat.getDrawable(App.INSTANCE, R.drawable.fabi_shape_corner40_05c));
    }

    private String getPayType() {
        StringBuilder payTypeRecord = new StringBuilder();
        if (payTypeWX.isChoose()) {
            payTypeRecord.append("1");
        }
        if (payTypeAL.isChoose()) {
            if (payTypeRecord.length() > 0) {
                payTypeRecord.append(",2");
            } else {
                payTypeRecord.append("2");
            }
        }
        if (payTypeYL.isChoose()) {
            if (payTypeRecord.length() > 0) {
                payTypeRecord.append(",3");
            } else {
                payTypeRecord.append("3");
            }
        }
        return payTypeRecord.toString();
    }

    private boolean checkParam() {
        String payTypeRecord = getPayType();  // 记录用户选择支付方式
        String typeVal = isBuy ? buyPrice : sellPrice;  // 估价
        String typeNumber = etNum.getText().toString().trim();  // 买入量
        String typeMinP = etMin.getText().toString().trim();  // 最小数量
        String typeMaxP = etMax.getText().toString().trim();  // 最大数量
        if (TextUtils.isEmpty(typeVal)) {
            ToastUtil.showShort("请输入单价");
            return false;
        }
        if (Double.valueOf(typeVal) <= 0) {
            ToastUtil.showShort("价格必须大于0");
            return false;
        }
        if (TextUtils.isEmpty(typeNumber)) {
            ToastUtil.showShort("请输入数量");
            return false;
        }
        if (TextUtils.isEmpty(typeNumber)) {
            ToastUtil.showShort("数量必须大于0");
            return false;
        }
        if (TextUtils.isEmpty(typeMinP)) {
            ToastUtil.showShort("请输入最小交易额");
            return false;
        }
        if (Double.valueOf(typeMinP) <= 0) {
            ToastUtil.showShort("最小交易额必须大于0");
            return false;
        }
        if (TextUtils.isEmpty(typeMaxP)) {
            ToastUtil.showShort("请输入最大交易额");
            return false;
        }
        if (Double.valueOf(typeMaxP) < Double.valueOf(typeMinP)) {
            ToastUtil.showShort("最大交易额必须大于等于最小交易额");
            return false;
        }
        String typePrice = new BigDecimal(etNum.getText().toString()).multiply(new BigDecimal(isBuy ? buyPrice : sellPrice)).toPlainString();  // 金额

        if (Double.valueOf(typeMaxP) > Double.valueOf(typePrice)) {
            ToastUtil.showShort("最大交易额不能大于交易额" + typePrice);
            return false;
        }
        if (!isBuy) {
            if (TextUtils.isEmpty(payTypeRecord)) {
                ToastUtil.showShort("请选择支付方式");
                return false;
            }
        }
        return true;
    }

    @Override
    protected void initData() {
        mPresenter.requsetPayWayList();
        mPresenter.requestUnitPrice(isBuy);
    }

    public void onPayWaySuccess(List<PayWayBean> data) {

        Flowable.fromIterable(data)
                .filter(payWayBean -> payWayBean.getPayType().equals(PayTypeEnum.WX.getType()) && !payWayBean.isDisable())
                .subscribe(payWayBean -> llWx.setVisibility(View.VISIBLE));
        Flowable.fromIterable(data)
                .filter(payWayBean -> payWayBean.getPayType().equals(PayTypeEnum.ALIPAY.getType()) && !payWayBean.isDisable())
                .subscribe(payWayBean -> llAlipay.setVisibility(View.VISIBLE));
        Flowable.fromIterable(data)
                .filter(payWayBean -> payWayBean.getPayType().equals(PayTypeEnum.YL.getType()) && !payWayBean.isDisable())
                .subscribe(payWayBean -> llYl.setVisibility(View.VISIBLE));
    }


    public void updateBuyPrice(String value) {
        buyPrice = value;
        if (isBuy) {
            tvPrice.setText(buyPrice);
        }
    }

    public void updateSellPrice(String value) {
        sellPrice = value;
        if (!isBuy) {
            tvPrice.setText(sellPrice);
        }
    }
}
